* Specify computer
local comp="davidbyrne10"

* Specify input and output directories
local datdir = "/Users/`comp'/Dropbox/Research/Billcap/Stata/Data/"
local figdir = "/Users/`comp'/Desktop/ReStatFigs/"
local paperdir= "/Users/`comp'/Dropbox/Research/Billcap/Writing_presenting/TellmeSomething/"


* IPWs
qui gen ipw=.
replace ipw=1 if read_month<=632
forval t=633(1)641){
	qui logit T preT_dailykWh use_above use_corr use_below Nqtr_start1-Nqtr_start6 `demogvars' if read_month==`t'
	qui predict ptmp if read_month==`t' 
	qui replace ipw=(T==1)*(1/ptmp)+(T==0)*(1/(1-ptmp)) if read_month==`t'
	drop ptmp
}

* Checking
*qui logit T preT_dailykWh use_above use_corr use_below Nqtr_start1-Nqtr_start6 `demogvars'  
*qui predict ptmp 
*qui replace ipw=(T==1)*(1/ptmp)+(T==0)*(1/(1-ptmp)) 
*drop ptmp	

qui sum median_HHincome, detail
gen inc=cond(median_HHincome>r(p50),1,0)
qui sum av_age, detail
gen age=cond(av_age>r(p50),1,0)		
qui sum PFullTime, detail
gen emp=cond(PFullTime>r(p50),1,0)
qui sum PRented, detail
gen rent=cond(PRented>r(p50),1,0)		
qui sum PRented, detail	
gen env=cond(green>r(p50),1,0)

* Pre-treatment dummies
replace first_treatment_date=td(01oct2012) if first_treatment_date<td(01oct2012)
replace first_treatment_date=td(30mar2013) if first_treatment_date>td(30mar2013) & first_treatment_date<td(1apr2013)
replace first_treatment_date=td(01jun2013) if first_treatment_date>td(15apr2013)
gen first_treatment_month=mofd(first_treatment_date)
gen diff_month=read_month-first_treatment_month
gen Tmneg3=cond(diff_month==-3 & T!=. & first_treatment_date<td(01jun2013),1,0)
gen Tmneg2=cond(diff_month==-2 & T!=. & first_treatment_date<td(01jun2013),1,0)
gen Tmneg1=cond(diff_month==-1 & T!=. & first_treatment_date<td(01jun2013),1,0)


* Blocks for identifying pre-treatment Tm3neg, Tm2neg Tm1neg
gen dec=cond(read_month==tm(2012m12),1,0)
gen jan=cond(read_month==tm(2013m1),1,0)
gen feb=cond(read_month==tm(2013m2),1,0)
gen mar=cond(read_month==tm(2013m3),1,0)
sort account_number read_month
by account_number: egen tot_dec=total(dec)
by account_number: egen tot_jan=total(jan)
by account_number: egen tot_feb=total(feb)
by account_number: egen tot_mar=total(mar)

* Blocks: before October 2012, non-treated between dec, jan, feb
gen block=cond(read_month<tm(2012m12),1,0)
replace block=block+cond(tot_dec==1 & tot_jan==1 & tot_feb==1 & tot_mar==1 & (dec==1 | jan==1 | feb==1),1,0)
replace block=0 if T==1 | T==.
replace block=1 if Tmneg1==1 & T==0
replace block=1 if Tmneg2==1 & T==0
replace block=1 if Tmneg3==1 & T==0


foreach var1 in T A Tm0 Tm1 Tm2 Tm3 Tm4 Tm5 Tm6 Tm7 postT1 postT2{
	foreach var2 in inc age emp rent env{
		qui gen `var1'__`var2'=`var1'*`var2'	
	}
}
foreach var1 in Tmneg1 Tmneg2 Tmneg3{
	foreach var2 in use_above use_corr use_below no_ans use_q1 use_q2 use_q3 use_q4 use_q5 inc age emp rent env postT1 postT2{
		qui gen `var1'__`var2'=`var1'*`var2'
	}
}

local treat="Tm0 Tm1 Tm2 Tm3 Tm4 Tm5 Tm6 Tm7"
local priors="Tm0__use_above Tm1__use_above Tm2__use_above Tm3__use_above Tm4__use_above Tm5__use_above Tm6__use_above Tm7__use_above Tm0__use_corr Tm1__use_corr Tm2__use_corr Tm3__use_corr Tm4__use_corr Tm5__use_corr Tm6__use_corr Tm7__use_corr Tm0__use_below Tm1__use_below Tm2__use_below Tm3__use_below Tm4__use_below Tm5__use_below Tm6__use_below Tm7__use_below Tm0__no_ans Tm1__no_ans Tm2__no_ans Tm3__no_ans Tm4__no_ans Tm5__no_ans Tm6__no_ans Tm7__no_ans"
local usage="Tm0__use_q1 Tm1__use_q1 Tm2__use_q1 Tm3__use_q1 Tm4__use_q1 Tm5__use_q1 Tm6__use_q1 Tm7__use_q1 Tm0__use_q2 Tm1__use_q2 Tm2__use_q2 Tm3__use_q2 Tm4__use_q2 Tm5__use_q2 Tm6__use_q2 Tm7__use_q2 Tm0__use_q4 Tm1__use_q4 Tm2__use_q4 Tm3__use_q4 Tm4__use_q4 Tm5__use_q4 Tm6__use_q4 Tm7__use_q4 Tm0__use_q5 Tm1__use_q5 Tm2__use_q5 Tm3__use_q5 Tm4__use_q5 Tm5__use_q5 Tm6__use_q5 Tm7__use_q5"
local demog="Tm0__inc Tm1__inc Tm2__inc Tm3__inc Tm4__inc Tm5__inc Tm6__inc Tm7__inc Tm0__age Tm1__age Tm2__age Tm3__age Tm4__age Tm5__age Tm6__age Tm7__age Tm0__emp Tm1__emp Tm2__emp Tm3__emp Tm4__emp Tm5__emp Tm6__emp Tm7__emp Tm0__rent Tm1__rent Tm2__rent Tm3__rent Tm4__rent Tm5__rent Tm6__rent Tm7__rent Tm0__env Tm1__env Tm2__env Tm3__env Tm4__env Tm5__env Tm6__env Tm7__env"
local varlist="`usage' `priors'"
local controls="read_month2-read_month11 postT1__use_above postT1__use_corr postT1__use_below postT2__use_above postT2__use_corr postT2__use_below postT1__use_q1 postT1__use_q2 postT1__use_q4 postT1__use_q5 postT2__use_q1 postT2__use_q2 postT2__use_q4 postT2__use_q5 postT1__inc postT1__age postT1__emp postT1__rent postT1__env postT2__inc postT2__age postT2__emp postT2__rent postT2__env"				
save `datdir'tempdat.dta, replace

local lev=95
qui parmby "areg ldailykWh `varlist' `controls' [pweight=ipw], absorb(account_number) cluster(account_number)", label norestore level(`lev')
keep if _n<=64
gen zero=0
gen period=1 if parmseq==1
replace period=period[_n-1]+1 if parmseq >=2 & parmseq<=8
replace period=period[_n-8] if parmseq>=9				
replace estimate=estimate*100
replace min`lev'=min`lev'*100
replace max`lev'=max`lev'*100
gen parmnum=1 if parmseq<=8
replace parmnum=parmnum[_n-8]+1 if parmseq>8
sort parmnum parmseq
save `datdir'sample_monthly_time2.dta, replace

* Estimate pre-treatment placebos in similar fashion
use `datdir'tempdat.dta, clear
sort account_number read_month
by account_number: gen trend=_n
foreach var1 in use_q1 use_q2 use_q3 use_q4 use_q5 use_above use_below use_corr no_ans inc age emp rent env{
	foreach var2 in read_month1 read_month2 read_month3 read_month4 read_month5 read_month6 read_month7 read_month8 read_month9 trend{
		gen `var1'__`var2'=`var1'*`var2'
	}
}

qui parmby "areg ldailykWh Tmneg3__use_q1 Tmneg2__use_q1 Tmneg1__use_q1 Tmneg3__use_q2 Tmneg2__use_q2 Tmneg1__use_q2 Tmneg3__use_q4 Tmneg2__use_q4 Tmneg1__use_q4 Tmneg3__use_q5 Tmneg2__use_q5 Tmneg1__use_q5 Tmneg3__use_above Tmneg2__use_above Tmneg1__use_above Tmneg3__use_corr Tmneg2__use_corr Tmneg1__use_corr Tmneg3__use_below Tmneg2__use_below Tmneg1__use_below Tmneg3 Tmneg2 Tmneg1 read_month2 read_month3 read_month4 read_month5 read_month6 read_month7 read_month8 read_month9 use_above__trend no_ans__trend use_corr__trend use_q1__read_month2 use_q1__read_month3 use_q1__read_month4 use_q1__read_month5 use_q1__read_month6 use_q1__read_month7 use_q1__read_month8 use_q2__read_month2 use_q2__read_month3 use_q2__read_month4 use_q2__read_month5 use_q2__read_month6 use_q2__read_month7 use_q2__read_month8 use_q3__read_month2 use_q3__read_month3 use_q3__read_month4 use_q3__read_month5 use_q3__read_month6 use_q3__read_month7 use_q3__read_month8 use_q4__read_month2 use_q4__read_month3 use_q4__read_month4 use_q4__read_month5 use_q4__read_month6 use_q4__read_month7 use_q4__read_month8 if block==1 [pweight=ipw], absorb(account_number) cluster(account_number)", label norestore level(`lev')
keep if _n<=24
gen zero=0
gen period=-2 if parmseq==1
replace period=period[_n-1]+1 if parmseq >=2 & parmseq<=3
replace period=period[_n-3] if parmseq>=4				
replace estimate=estimate*100
replace min`lev'=min`lev'*100
replace max`lev'=max`lev'*100	
gen parmnum=1 if parmseq<=3
replace parmnum=parmnum[_n-3]+1 if parmseq>3	
replace parmseq=parmseq-100
sort parmnum parmseq
merge m:m parmnum parmseq using `datdir'sample_monthly_time2.dta
sort parmnum parmseq
by parmnum: replace parmseq=_n
by parmnum: drop if _n==_N
save `datdir'sample_monthly_time2.dta, replace	
